package com.dqp.cslggroup.JWXT;


import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.CheckPassword;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.Date;
import java.util.Map;

public class ChangePassword {
    private String url;
    private String old_pwd;
    private String new_pwd;
    private String stuNum;
    private String pwd_check;
    private Map<String, String> cookies;
    private String Authorization = SharedPreferencesUtil.getStringSp(MyApplication.getContext(), "Authorization", "");


    ChangePassword(String url, String stuNum, Map<String, String> cookies, String old_pwd, String new_pwd) {
        this.cookies = cookies;
        this.new_pwd = new_pwd;
        this.old_pwd = old_pwd;
        this.url = url;
        this.stuNum = stuNum;
        String check = CheckPassword.checkPassword(new_pwd);
        if (check.contains("强"))
            pwd_check = "3";
        else if (check.contains("中"))
            pwd_check = "2";
        else if (check.contains("弱"))
            pwd_check = "1";
        else pwd_check = "1";
    }

    int change() throws Exception {
        String time = String.valueOf(new Date().getTime());
        Connection connection = Jsoup.connect(url + "/jwglxt/xtgl/mmgl_xgMm.html?time=" + time + "&gnmkdm=index&su=" + stuNum);
        connection.header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        connection.header("Authorization", Authorization);
        connection.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
        connection.data("time", time);
        connection.data("gnmkdm", "index");
        connection.data("su", stuNum);
        connection.cookies(cookies)
                .ignoreContentType(true)
                .method(Connection.Method.POST)
                .timeout(5000)
                .execute();

        connection = Jsoup.connect(url + "/jwglxt/xtgl/mmgl_cxCheckYhMm.html?gnmkdm=index&su=" + stuNum);
        connection.header("Authorization", Authorization);
        connection.header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        connection.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
        connection.data("gnmkdm", "index");
        connection.data("su", stuNum);
        connection.data("kl", old_pwd);
        connection.cookies(cookies)
                .ignoreContentType(true)
                .method(Connection.Method.POST)
                .timeout(5000)
                .execute();

        connection = Jsoup.connect(url + "/jwglxt/xtgl/mmgl_cxCheckYhMm.html?gnmkdm=index&su=" + stuNum);
        connection.header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        connection.header("Authorization", Authorization);
        connection.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
        connection.data("gnmkdm", "index");
        connection.data("su", stuNum);
        connection.data("kl", old_pwd);
        connection.cookies(cookies)
                .ignoreContentType(true)
                .method(Connection.Method.POST)
                .timeout(5000)
                .execute();
        Connection.Response response = connection.execute();
        Document document = Jsoup.parse(response.body());

        connection = Jsoup.connect(url + "/jwglxt/xtgl/mmgl_xgMm.html?gnmkdm=index&su=" + stuNum);
        connection.header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        connection.header("Authorization", Authorization);
        connection.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
        connection.data("gnmkdm", "index");
        connection.data("su", stuNum);
        connection.data("doType", "save");
        connection.data("ymm", old_pwd);
        connection.data("mm", new_pwd);
        connection.data("yhmmdj", pwd_check);
        connection.data("nmm", new_pwd);
        connection.cookies(cookies)
                .ignoreContentType(true)
                .method(Connection.Method.POST)
                .timeout(5000)
                .execute();
        if (document !=null && document.text().contains("true")) {
            return 1;
        }
        return 0;
    }

}
